package com.slz3.business.module.socialManagement.socDianzan.steward;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.response.RR;
import com.slz3.business.frame.response.ResInfo;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.socialManagement.socDianzan.domain.SocDianzan;
import com.slz3.business.module.socialManagement.socDianzan.domain.SocDianzanFactory;
import org.springframework.data.domain.Page;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.util.List;

public class SocDianzanResExecutor {

    static Mono<ServerResponse> add(ServerRequest req, SocDianzan socDianzan) {
        SocDianzan socDianzanDBLatest = Services.socDianzan.save(socDianzan);
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(socDianzanDBLatest).build());
    }

    /**
     * known: socDianzanList is not null
     */
    public static Mono<ServerResponse> delete(ServerRequest req) {
        List<SocDianzan> socDianzanList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(socDianzanList)) {
            List<SocDianzan> socDianzanListNew = Services.socDianzan.delete(socDianzanList);
            if (VE.isNotEmpty(socDianzanListNew) && socDianzanListNew.size() == socDianzanList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(socDianzanListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("delete fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    static Mono<ServerResponse> modify(ServerRequest req, SocDianzan source) {
        SocDianzan target = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (target != null) {
            Steward.merge(source, target);
            SocDianzan socDianzanDBLatest = Services.socDianzan.modify(target);
            return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(socDianzanDBLatest).build());
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    static Mono<ServerResponse> list(ServerRequest req) {
        Page<SocDianzan> socDianzanPage = Services.socDianzan.page(req.queryParams());
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).theme("page").page(socDianzanPage).build());
    }

    static Mono<ServerResponse> info(ServerRequest req) {
        SocDianzan target = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (target != null) {
            return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(target).build());
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    static Mono<ServerResponse> choice(ServerRequest req) {
        Page<SocDianzan> socDianzanPage = Services.socDianzan.page(req.queryParams());
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).theme("page").page(socDianzanPage).build());
    }

    /**
     * known: socDianzanList is not null
     */
    public static Mono<ServerResponse> auditSubmit(ServerRequest req) {
        List<SocDianzan> socDianzanList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(socDianzanList)) {
            socDianzanList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.WAIT_AUDIT);
                SocDianzanFactory.initBaseFields(item);
            });
            List<SocDianzan> socDianzanListNew = Services.socDianzanTransactional.batchModify(socDianzanList);
            if (VE.isNotEmpty(socDianzanListNew) && socDianzanListNew.size() == socDianzanList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(socDianzanListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditSubmit fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: socDianzanList is not null
     */
    public static Mono<ServerResponse> audit(ServerRequest req) {
        List<SocDianzan> socDianzanList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(socDianzanList)) {
            socDianzanList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.APPROVED);
                SocDianzanFactory.initBaseFields(item);
            });
            List<SocDianzan> socDianzanListNew = Services.socDianzanTransactional.batchModify(socDianzanList);
            if (VE.isNotEmpty(socDianzanListNew) && socDianzanListNew.size() == socDianzanList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(socDianzanListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("audit fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: socDianzanList is not null
     */
    public static Mono<ServerResponse> auditReject(ServerRequest req) {
        List<SocDianzan> socDianzanList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(socDianzanList)) {
            socDianzanList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.REJECT);
                SocDianzanFactory.initBaseFields(item);
            });
            List<SocDianzan> socDianzanListNew = Services.socDianzanTransactional.batchModify(socDianzanList);
            if (VE.isNotEmpty(socDianzanListNew) && socDianzanListNew.size() == socDianzanList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(socDianzanListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditReject fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: socDianzanList is not null
     */
    public static Mono<ServerResponse> usingEnable(ServerRequest req) {
        List<SocDianzan> socDianzanList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(socDianzanList)) {
            socDianzanList.stream().forEach(item -> {
                item.setSerUsing(UsingFlag.ENABLE);
                SocDianzanFactory.initBaseFields(item);
            });
            List<SocDianzan> socDianzanListNew = Services.socDianzanTransactional.batchModify(socDianzanList);
            if (VE.isNotEmpty(socDianzanListNew) && socDianzanListNew.size() == socDianzanList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(socDianzanListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditReject fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }


    /**
     * known: socDianzanList is not null
     */
    public static Mono<ServerResponse> usingDisable(ServerRequest req) {
        List<SocDianzan> socDianzanList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(socDianzanList)) {
            socDianzanList.stream().forEach(item -> {
                item.setSerUsing(UsingFlag.DISABLE);
                SocDianzanFactory.initBaseFields(item);
            });
            List<SocDianzan> socDianzanListNew = Services.socDianzanTransactional.batchModify(socDianzanList);
            if (VE.isNotEmpty(socDianzanListNew) && socDianzanListNew.size() == socDianzanList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(socDianzanListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("usingDisable fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

}
